import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/live/live_chat_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/expert/items/expert_logo.dart';
import 'package:ball_lion/module/live/items/live_message_list.dart';
import 'package:ball_lion/module/overlay/match_tip_float_widget.dart';
import 'package:visibility_detector/visibility_detector.dart';
import 'package:ball_lion/module/live/live_chat_logic.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class LiveChatView extends StatefulWidget {
  const LiveChatView({super.key, this.matchId});

  final int? matchId;

  @override
  State<LiveChatView> createState() => _LiveChatViewState();
}

class _LiveChatViewState extends State<LiveChatView>
    with AutomaticKeepAliveClientMixin {
  late LiveChatLogic logic;

  @override
  void initState() {
    GetNotifyConfig.routesIgnore.add(Get.currentRoute);
    logic = Get.put(LiveChatLogic(), tag: "${widget.matchId}");
    logic.matchId = "${widget.matchId}";
    super.initState();
  }

  @override
  void dispose() {
    GetNotifyConfig.routesIgnore.remove(Get.currentRoute);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetBuilder<LiveChatLogic>(
      tag: "${widget.matchId}",
      builder: (logic) => VisibilityDetector(
        key: const Key("chat"),
        onVisibilityChanged: (VisibilityInfo info) {
          if (!info.visibleBounds.isEmpty) {
            logic.getFocus();
          }
        },
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          body: SafeArea(
            top: false,
            child: Stack(
              children: [
                Positioned.fill(
                  child: Image.asset(
                    Utils.getImgPath("chat_background.png"),
                    alignment: Alignment.topCenter,
                    fit: BoxFit.cover,
                  ),
                ),
                if (logic.liveInfo != null)
                  Positioned.fill(
                    child: LiveMessageList(
                      key: ValueKey(logic.liveInfo?.liveId),
                      scene: MessageScene.match,
                      liveDetail: logic.liveInfo,
                      sportsId: logic.sportsId,
                    ),
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget focus() {
    return Container(
      width: 56,
      clipBehavior: Clip.antiAlias,
      decoration: const BoxDecoration(
          color: Colours.white,
          borderRadius: BorderRadius.vertical(
              top: Radius.circular(28), bottom: Radius.circular(8))),
      child: Column(
        children: [
          SizedBox(
              height: 62,
              child: Stack(
                alignment: AlignmentDirectional.center,
                children: [
                  GestureDetector(
                      onTap: () {
                        Utils.onEvent('hpzb', params: {'hpzb': 2});
                        Get.toNamed(Routes.userPage,
                            arguments: logic.liveInfo?.expertId,
                            parameters: {"type": "2"});
                      },
                      child: ExpertLogo(
                          logo: logic.liveInfo?.expertLogo, inLive: true)),
                  Positioned(
                    bottom: 0,
                    child: GestureDetector(
                      onTap: logic.toFocus,
                      child: Container(
                        width: 40,
                        height: 24,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: logic.liveInfo?.isFocus == 1
                                ? Colours.greyDD
                                : null,
                            borderRadius: BorderRadius.circular(18),
                            gradient: logic.liveInfo?.isFocus == 1
                                ? null
                                : const LinearGradient(
                                    colors: [
                                      Color(0xFFF54D4F),
                                      Color(0xFFF53C6A)
                                    ],
                                  )),
                        child: QsText(
                          logic.liveInfo?.isFocus == 1 ? "已关注" : "+关注",
                          fontSize: 10,
                          color: logic.liveInfo?.isFocus == 1
                              ? Colours.grey99
                              : Colours.white,
                        ),
                      ),
                    ),
                  )
                ],
              )),
          QsText(logic.liveInfo?.expertName ?? "",
                  maxLines: 1,
                  textAlign: TextAlign.center,
                  needCenter: true,
                  fontSize: 10,
                  fontWeight: FontWeight.w500)
              .paddingSymmetric(horizontal: 2, vertical: 4)
        ],
      ),
    );
  }

  Widget message(LiveChatEntity message) {
    return RichText(
        text: TextSpan(
            style: const TextStyle(fontSize: 14, color: Colours.text),
            children: [
          if (message.userName.hasValue)
            TextSpan(
                text:
                    "${message.userName}${message.type == ChatMessageType.comment ? "：" : " "}",
                style: const TextStyle(color: Colours.grey99)),
          TextSpan(
              text: message.message,
              style: TextStyle(color: message.type?.color))
        ]));
  }

  @override
  bool get wantKeepAlive => true;
}
